package com.cuitrip.im;

import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by MiJack on 2016/1/29.
 */

public class CtParam implements Serializable {
    //        {\"cuitrip\":\"1\",\"key\":\"%@\",\"replace\":{\"nick\":\"%@\",\"person\":\"%@\",\"date\":\"%@\"}}
    public String cuitrip;
    public String key;
    public JSONObject replace;

    public static final String ORDER_KEY = "1";

    public static final String NICK_KEY = "[NICK]";
    public static final String DATE_KEY = "[DATE]";
    public static final String PERSON_KEY = "[PERSON]";

    public CtParam() {
    }

    public CtParam(String cuitrip, String key, HashMap<String, String> replace) {
        this.cuitrip = cuitrip;
        this.key = key;
        this.replace = new JSONObject();
        for (Map.Entry<String, String> entry : replace.entrySet()) {
            this.replace.put(entry.getKey(), entry.getValue());
        }
    }

    public String toJSONString() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("key", key);
        jsonObject.put("cuitrip", cuitrip);
        jsonObject.put("replace", replace);
        return jsonObject.toJSONString();
    }

    public static CtParam parseObject(String extra) {
        CtParam ctParam = new CtParam();
        JSONObject jsonObject = (JSONObject) JSONObject.parse(extra);
        try {
            ctParam.key = jsonObject.getString("key");
            ctParam.cuitrip = jsonObject.getString("cuitrip");
            ctParam.replace = jsonObject.getJSONObject("replace");
        } catch (Exception e) {

        }
        return ctParam;
    }
}